package com.neuedu.ud;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet(value = "/fileUpload")
public class FileUploadController  extends HttpServlet {

    public static final String  baseDir = "D:\\upload";


     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        boolean isMultipart = ServletFileUpload.isMultipartContent(request);

        if (isMultipart) {

            // Create a factory for disk-based file items
            DiskFileItemFactory factory = new DiskFileItemFactory();

            // Configure a repository (to ensure a secure temp location is used)
            ServletContext servletContext = this.getServletConfig().getServletContext();
            File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
            factory.setRepository(repository);

            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);
            // Parse the request
            try {

                //表单域， input【text、file】
                List<FileItem> items = upload.parseRequest(request);
                for (FileItem item : items) {

                    if(item.isFormField()) { // text password checkbox ...
                        String name = item.getFieldName() ;// 控件的名字 input[name= ??? ]
                        String value = item.getString(); // 控件的值 input[value= ??? ]
                        System.out.println(name+"\t"+value);
                    }else{ //   type =  file

                        //                        String fileName = item.getFieldName(); //原始的文件名
                        String fileName = item.getName(); //原始的文件名
                        File dist = new File(baseDir,fileName);
                        item.write(dist);//转储到 baseDir【D:/upload】


                        String now = "";//new Date();
                        String clientHost = request.getRemoteHost();
                        long size = item.getSize();

                        String info = String.format("host:%s\t time:%s, name:%s\t size:%s",
                                clientHost,
                                new Date().toString(),
                                fileName,
                                String.valueOf(size)
                        );

                        //上传文件的信息
                        System.out.println(info);
                    }
                }
            } catch (FileUploadException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }


        } else {//普通的表单处理
            //            request.getParameter()

        }

    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }



}
